<template>
    <div style="margin-top: 60px;margin-left:80px;border: 0px solid red;" >
        <div class="user-info">
            <el-link icon="el-icon-back" @click="goto" style="position: relative;left: -1230px">chat</el-link>
            <div class="avatar" style="position: relative;left: 20px;">U</div>
            <el-dropdown style="position: relative;left: 20px;">
                  <span class="el-dropdown-link">
                    {{user.username}}<i class="el-icon-arrow-down el-icon--right home_userinfo"></i>
                  </span>
                <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
        </div>
        <el-form label-width="100px" class="demo-ruleForm">
            <el-form-item label="关键字：" prop="keyWord">
                <el-input clearable v-model="keyWord" placeholder="请输入关键字" style="width: 230px;float: left"></el-input>
                <span style="margin-left: 0px">检索条件：</span>
                <el-select v-model="type" clearable placeholder="请选择条件">
                    <el-option label="问题" value="question"></el-option>
                    <el-option label="答案" value="answer"></el-option>
                </el-select>
                <el-button type="primary" icon="el-icon-search" style="float: right;position: relative;right: 280px;" @click="search()">搜索</el-button>
                <div class="block" style="float: right;margin-right: 320px">
                    <span class="demonstration">选择日期：</span>
                    <el-date-picker
                            v-model="date"
                            align="right"
                            type="date"
                            placeholder="选择日期"
                            :picker-options="pickerOptions">
                    </el-date-picker>
                </div>
            </el-form-item>
        </el-form>

        <el-table
                :data="tableData"
                border
                stripe
                style="width: 100%">
            <el-table-column
                    fixed
                    prop="id"
                    label="编号"
                    width="130">
            </el-table-column>
            <el-table-column
                    prop="username"
                    label="用户名"
                    width="130">
            </el-table-column>
            <el-table-column
                    prop="question"
                    label="问题"
                    width="360">
            </el-table-column>
            <el-table-column
                    prop="answer"
                    label="回答"
                    width="500">
            </el-table-column>
            <el-table-column
                    prop="createTime"
                    label="创建时间"
                    width="180">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="danger"
                            @click="del(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination style="margin-top: 20px;float: right"
                       background
                       layout="prev, pager, next"
                       :page-size="pageSize"
                       :total="total"
                       :current-page.sync="currentPage"
                       @current-change="page">
        </el-pagination>
    </div>
</template>

<script>
    export default {
        name: "Record",
        data() {
            return {
                tableData:null,
                currentPage:1,
                pageSize:5,
                total:null,
                keyWord:'',
                type:'',
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    },
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    }]
                },
                date: new Date(),
                user: ''
            }
        },
        methods:{
            goto(){
                this.$router.replace({path: '/chat'})
            },
            logout(){
                let _this = this;
                this.$confirm('注销登录吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    localStorage.removeItem('user')
                    _this.$router.replace({path: '/login'})
                })
            },
           page(currentPage){
               const _this = this
               const obj = {
                   page:currentPage,
                   size:_this.pageSize
               }
               axios.get('http://localhost:8181/record/list',{params:obj}).then(function (resp) {
                   _this.tableData = resp.data.data
                   _this.pageSize = resp.data.size
                   _this.total = resp.data.total
               })
           },
            search(){
                const _this = this
                //让翻页复原
                _this.currentPage=1
                const obj = {
                    page:1,
                    size:_this.pageSize,
                    keyWord:_this.keyWord,
                    type:_this.type,
                    date:formatDate(_this.date)
                }
                axios.get('http://localhost:8181/record/list',{params:obj}).then(function (resp) {
                    _this.tableData = resp.data.data
                    _this.pageSize = resp.data.size
                    _this.total = resp.data.total
                })
            },
            del(row) {
                const _this = this
                this.$confirm('确认删除【'+row.id+'号记录】吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.delete('http://localhost:8181/record/delete/'+row.id).then(function (resp) {
                        if(resp.data == 1){
                            _this.$alert('【'+row.id+'号记录】已删除', '', {
                                confirmButtonText: '确定',
                                callback: action => {
                                    location.reload()
                                }
                            });
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        },
        created() {
            const _this = this
            const obj = {
                page:1,
                size:_this.pageSize
            }
            axios.get('http://localhost:8181/record/list',{params:obj}).then(function (resp) {
                _this.tableData = resp.data.data
                _this.pageSize = resp.data.size
                _this.total = resp.data.total
            })
        },
        mounted: function () {
            this.user = JSON.parse(window.localStorage.getItem('user'))
        }
    }
    function formatDate(date) {
        if(date == null) return
        function padZero(num) {
            return num < 10 ? '0' + num : num;
        }

        var year = date.getFullYear();
        var month = padZero(date.getMonth() + 1);
        var day = padZero(date.getDate());
        var hours = padZero(date.getHours());
        var minutes = padZero(date.getMinutes());
        var seconds = padZero(date.getSeconds());

        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    }
</script>

<style>
    /* 头像样式 */
    .avatar {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        background: #007bff;
        color: white;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 10px;
        font-weight: bold;
    }

    .user-info {
        position: absolute;
        top: 10px;
        right: 60px;
        display: flex;
        align-items: center;
    }
</style>
